package hello.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.ResourceBundle;

@Configuration
public class JedisConfiguration {

    @Bean
    public JedisPool getJedisPool() {
        ResourceBundle rb = ResourceBundle.getBundle("jedis");
        String host = rb.getString("host");
        int port = Integer.parseInt(rb.getString("port"));
        int maxTotal = Integer.parseInt(rb.getString("maxTotal"));
        int maxWaitMillis = Integer.parseInt(rb.getString("maxWaitMillis"));

        JedisPoolConfig jpc = new JedisPoolConfig();
        jpc.setMaxTotal(maxTotal);
        jpc.setMaxWaitMillis(maxWaitMillis);
        return new JedisPool(jpc, host, port);
    }

    @Bean
    public Jedis getJedis(JedisPool jedisPool) {
        return jedisPool.getResource();
    }
}
